package com.assessment.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.assessment.dao.AssessmentProjectRepository;
import com.assessment.dao.AssessmentTemplateRepository;
import com.assessment.entity.AssessmentProject;
import com.assessment.entity.AssessmentTemplate;
import com.staff.dao.StaffRepository;
import com.staff.entity.Staff;

@Service
@Transactional
public class AssessmentProjectService implements IAssessmentProjectService {

	@Autowired
	private AssessmentProjectRepository assessmentProjectRepository;
	@Autowired
	private AssessmentTemplateRepository assessmentTemplateRepository;
	@Autowired
	private StaffRepository staffRepository;
	
	
	public void saveOrUpdate(AssessmentProject assessmentProject) {
		// TODO Auto-generated method stub
		Staff superior=staffRepository.findOne(assessmentProject.getSuperiorId());
		AssessmentTemplate assessmentTemplate =assessmentTemplateRepository.findOne(assessmentProject.getAssessmentTemplateId());
		assessmentProject.setSuperior(superior);
		assessmentProject.setAssessmentTemplate(assessmentTemplate);
		assessmentProjectRepository.save(assessmentProject);
	}

	public void delete(Long id) {
		// TODO Auto-generated method stub
		AssessmentProject assessmentProject=assessmentProjectRepository.findOne(id);
		if(assessmentProject!=null) {
			assessmentProject.setAssessmentTemplate(null);
			assessmentProject.setSuperior(null);
			assessmentProjectRepository.save(assessmentProject);
			assessmentProjectRepository.delete(assessmentProject);
		}		
	}

	public void delete(List<Long> ids) {
		// TODO Auto-generated method stub
		List<AssessmentProject> assessmentProjectList=(List<AssessmentProject>) assessmentProjectRepository.findAll(ids);
		if(assessmentProjectList!=null) {
			for(AssessmentProject assessmentProject :assessmentProjectList) {
				assessmentProject.setAssessmentTemplate(null);
				assessmentProject.setSuperior(null);
				assessmentProjectRepository.save(assessmentProject);
			}
			assessmentProjectRepository.delete(assessmentProjectList);
		}		
	}
	@Transactional(readOnly=true)
	public AssessmentProject findOne(Long id) {
		// TODO Auto-generated method stub
		return assessmentProjectRepository.findOne(id);
	}
	
	@Transactional(readOnly=true)
	public List<AssessmentProject> findAll() {
		// TODO Auto-generated method stub
		return (List<AssessmentProject>) assessmentProjectRepository.findAll();
	}
	@Transactional(readOnly=true)
	public Page<AssessmentProject> findAll(Specification<AssessmentProject> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<AssessmentProject> page=assessmentProjectRepository.findAll(spec,pageable);
		List<AssessmentProject> list=page.getContent();
		for(int i=0;i<list.size();i++) {
			Long superiorId= list.get(i).getSuperior().getId();
			String superiorName=list.get(i).getSuperior().getStaffName();
			Long assessmentTemplateId=list.get(i).getAssessmentTemplate().getId();	
			String assessmentTemplateName=list.get(i).getAssessmentTemplate().getName();
			list.get(i).setSuperiorId(superiorId);
			list.get(i).setSuperiorName(superiorName);
			list.get(i).setAssessmentTemplateId(assessmentTemplateId);
			list.get(i).setAssessmentTemplateName(assessmentTemplateName);
			Date data=new Date();
			if(list.get(i).getStarTime()!=null&&list.get(i).getEndTime()!=null) {
				if(data.after(list.get(i).getStarTime())&&data.before(list.get(i).getEndTime())) {			
					list.get(i).setExamine("执行");
				}
				else if(data.before(list.get(i).getStarTime())) {
					list.get(i).setExamine("未开始");
				}
				else if(data.after(list.get(i).getEndTime())){
					list.get(i).setExamine("结束");
				}
			}
			else {
				list.get(i).setExamine("未设定");
			}
		}
		Page<AssessmentProject> page1 = new PageImpl<AssessmentProject>(list,pageable, page.getTotalElements());
		return page1;
	}

}
